package com.sfy.mallchat.common.user.service.adapter;


import cn.hutool.core.util.ObjectUtil;
import com.sfy.mallchat.common.common.enums.YesOrNoEnum;
import com.sfy.mallchat.common.user.domain.entity.ItemConfig;
import com.sfy.mallchat.common.user.domain.entity.User;
import com.sfy.mallchat.common.user.domain.entity.UserBackpack;
import com.sfy.mallchat.common.user.domain.vo.resp.BadgeResp;
import com.sfy.mallchat.common.user.domain.vo.resp.UserInfoResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 组合器
 */
@Slf4j
public class UserAdapter {
    public static UserInfoResp buildUserInfo(User user, Integer modifyNameCount) {
        UserInfoResp vo = new UserInfoResp();
        BeanUtils.copyProperties(user,vo);
        vo.setId(user.getId());
        vo.setModifyNameChance(modifyNameCount);
        return vo;
    }

    public static List<BadgeResp> buildBadgeResp(List<ItemConfig> itemConfigs, List<UserBackpack> backpacks, User user) {
        if(ObjectUtil.isNull(user)){
            return Collections.emptyList();
        }
        Set<Long> obtainItemSet = backpacks.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());
        return itemConfigs.stream().map(a->{
            BadgeResp badgeResp = new BadgeResp();
            BeanUtils.copyProperties(a,badgeResp);
            badgeResp.setObtain(obtainItemSet.contains(a.getId())? YesOrNoEnum.YES.getStatus():YesOrNoEnum.NO.getStatus());
            badgeResp.setWearing(ObjectUtil.equal(a.getId(),user.getItemId())?YesOrNoEnum.YES.getStatus():YesOrNoEnum.NO.getStatus());
            return badgeResp;
            // 在徽章展示时，会先展示佩戴的，再展示拥有的，由于0是没有佩戴，所有要倒序
        }).sorted(Comparator.comparing(BadgeResp::getWearing,Comparator.reverseOrder())
                .thenComparing(BadgeResp::getObtain,Comparator.reverseOrder()))
                .collect(Collectors.toList());
    }
}
